package com.muyu.system.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.system.domain.DmUserAddress;
import com.muyu.system.service.IDmUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户地址Controller
 *
 * @author ruoyi
 * @date 2025-01-17
 */
@RestController
@RequestMapping("address")
public class DmUserAddressController extends BaseController {
    @Autowired
    private IDmUserAddressService dmUserAddressService;

    /**
     * 查询用户地址列表
     */
    //@RequiresPermissions("system:address:list")
    @GetMapping("/list")
    public Result list(DmUserAddress dmUserAddress) {
        startPage();
        List<DmUserAddress> list = dmUserAddressService.selectDmUserAddressList(dmUserAddress);
        return getDataTable(list);
    }

    /**
     * 导出用户地址列表
     */
    //@RequiresPermissions("system:address:export")
    @Log(title = "用户地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmUserAddress dmUserAddress) {
        List<DmUserAddress> list = dmUserAddressService.selectDmUserAddressList(dmUserAddress);
        ExcelUtil<DmUserAddress> util = new ExcelUtil<DmUserAddress>(DmUserAddress.class);
        util.exportExcel(response, list, "用户地址数据");
    }

    /**
     * 获取用户地址详细信息
     */
    //@RequiresPermissions("system:address:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return Result.success(dmUserAddressService.selectDmUserAddressById(id));
    }

    /**
     * 新增用户地址
     */
    //@RequiresPermissions("system:address:add")
    @Log(title = "用户地址", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DmUserAddress dmUserAddress) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        dmUserAddress.setUserId(loginUser.getUserid());
        return Result.success(dmUserAddressService.insertDmUserAddress(dmUserAddress), "新增地址成功");
    }

    /**
     * 修改用户地址
     */
    //@RequiresPermissions("system:address:edit")
    @Log(title = "用户地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DmUserAddress dmUserAddress) {
        return Result.success(dmUserAddressService.updateDmUserAddress(dmUserAddress), "修改地址成功");
    }

    /**
     * 删除用户地址
     */
    //@RequiresPermissions("system:address:remove")
    @Log(title = "用户地址", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return Result.success(dmUserAddressService.deleteDmUserAddressByIds(ids), "删除地址成功");
    }
}
